
/*    This file is part of the managarm operating system.
 *   Copyright (C) 2007, 2008, 2009  Alexander van der Grinten
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

package org.managarm.jmanaintlib.machine;

// import managarm virtual architecture
import org.managarm.jmanavalib.core.VaDatatype;
import org.managarm.jmanavalib.core.VaLiteral;

public final class VmUtils {
	// creates a new cell for store a value of data type "type"
	// and loads zero or null into it
	public static final VmCell createCell(final VaDatatype type) {
		if(type instanceof VaDatatype.DtPrimitive) {
			final VaDatatype.DtPrimitive prim = (VaDatatype.DtPrimitive)type;
			
			final int index = prim.identity;
			
			switch(index) {
			case VaDatatype.DtPrimitive.PRIM_BYTE:
				return new VmCellInt(0);
			case VaDatatype.DtPrimitive.PRIM_SHORT:
				return new VmCellInt(0);
			case VaDatatype.DtPrimitive.PRIM_INT:
				return new VmCellInt(0);
			case VaDatatype.DtPrimitive.PRIM_UBYTE:
				return new VmCellUInt(0);
			case VaDatatype.DtPrimitive.PRIM_USHORT:
				return new VmCellUInt(0);
			case VaDatatype.DtPrimitive.PRIM_UINT:
				return new VmCellUInt(0);
			case VaDatatype.DtPrimitive.PRIM_CHAR:
				return new VmCellChar(0);
			case VaDatatype.DtPrimitive.PRIM_BOOLEAN:
				return new VmCellBoolean(false);
			case VaDatatype.DtPrimitive.PRIM_NULL:
				return new VmCellNull();
			}
		}else if(type instanceof VaDatatype.DtArray) {
			return new VmCellNull();
		}else if(type instanceof VaDatatype.DtClass) {
			return new VmCellNull();
		}
		
		// we could not process the data type
		throw new UnsupportedOperationException("Unknown data type");
	}
	
	// creates a cell to store a literal
	public static final VmCell createCell(VaLiteral lit) throws VmError {
		if(lit instanceof VaLiteral.LitNull) {
			return new VmCellNull();
		}else if(lit instanceof VaLiteral.LitUInt) {
			final VaLiteral.LitUInt literal = (VaLiteral.LitUInt)lit;
			return new VmCellUInt(literal.value);
		}else if(lit instanceof VaLiteral.LitChar) {
			final VaLiteral.LitChar literal = (VaLiteral.LitChar)lit;
			return new VmCellChar(literal.value);
		}else if(lit instanceof VaLiteral.LitBoolean) {
			final VaLiteral.LitBoolean literal = (VaLiteral.LitBoolean)lit;
			return new VmCellBoolean(literal.value);
		}else if(lit instanceof VaLiteral.LitCharArray) {
			final VaLiteral.LitCharArray literal = (VaLiteral.LitCharArray)lit;
			final VmCellArray cell = new VmCellArray(new VaDatatype.DtArray
					(new VaDatatype.DtPrimitive(VaDatatype.DtPrimitive.PRIM_CHAR), 0),
					literal.size());
			for(int i = 0; i < literal.size(); i++)
				cell.storeElement(i, new VmCellChar(literal.get(i)));
			return cell;
		}else{
			throw new VmError("Invalid literal "
					+ lit.getClass().getName());
		}
	}
}
